package cn.chiship.framework.business.biz.business.service.impl;

import cn.chiship.framework.business.biz.business.entity.BusinessOrderHeader;
import cn.chiship.framework.business.biz.business.mapper.BusinessOrderHeaderMapper;
import cn.chiship.framework.business.biz.business.pojo.dto.BusinessOrderPaymentRecordDto;
import cn.chiship.framework.business.biz.business.service.BusinessOrderHeaderService;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.framework.base.BaseServiceImpl;
import cn.chiship.framework.business.biz.business.mapper.BusinessOrderPaymentRecordMapper;
import cn.chiship.framework.business.biz.business.entity.BusinessOrderPaymentRecord;
import cn.chiship.framework.business.biz.business.entity.BusinessOrderPaymentRecordExample;
import cn.chiship.framework.business.biz.business.service.BusinessOrderPaymentRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单支付业务接口实现层
 * 2023/3/26
 *
 * @author lijian
 */
@Service
public class BusinessOrderPaymentRecordServiceImpl extends BaseServiceImpl<BusinessOrderPaymentRecord, BusinessOrderPaymentRecordExample> implements BusinessOrderPaymentRecordService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessOrderPaymentRecordServiceImpl.class);

    @Resource
    BusinessOrderPaymentRecordMapper businessOrderPaymentRecordMapper;
    @Resource
    BusinessOrderHeaderMapper businessOrderHeaderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResult payNotify(BusinessOrderPaymentRecordDto orderPaymentRecordDto) {
        Byte payStatus = orderPaymentRecordDto.getPayStatus();
        if (Byte.valueOf("0").equals(payStatus)) {
            BusinessOrderHeader userOrderHeader = new BusinessOrderHeader();
            userOrderHeader.setId(orderPaymentRecordDto.getOrderId());
            userOrderHeader.setOrderStatus((byte) 2);
            businessOrderHeaderMapper.updateByPrimaryKeySelective(userOrderHeader);
            BusinessOrderPaymentRecord orderPaymentRecord = new BusinessOrderPaymentRecord();
            BeanUtils.copyProperties(orderPaymentRecordDto, orderPaymentRecord);
            BusinessOrderPaymentRecordExample userOrderPaymentRecordExample = new BusinessOrderPaymentRecordExample();
            userOrderPaymentRecordExample.createCriteria().andTradeNoEqualTo(orderPaymentRecordDto.getTradeNo());
            if (businessOrderPaymentRecordMapper.countByExample(userOrderPaymentRecordExample) == 0) {
                super.insertSelective(orderPaymentRecord);
            }
        } else {
            BusinessOrderHeader userOrderHeader = new BusinessOrderHeader();
            userOrderHeader.setId(orderPaymentRecordDto.getOrderId());
            userOrderHeader.setOrderStatus((byte) 1);
            businessOrderHeaderMapper.updateByPrimaryKeySelective(userOrderHeader);
        }
        return BaseResult.ok();
    }
}
